<?php

// 发立得信息发布系统房屋信息版(php+mysql) V1.0
// 演示地址: http://fang.chalide.cn
// 文件路径: usr/mima.php
// 文件大小: 4771 字节
// 最后修改时间: 2025-05-23 16:35:40
// 作者: yujianyue
// 邮件: 15058593138@qq.com
// 版权所有,保留发行权和署名权
/**
 * 本文件功能：找回密码
 * 版权声明：保留发行权和署名权
 * 作者信息：15058593138@qq.com
 */

// 处理AJAX请求
if (isset($_GET['act'])) {
    $act = $_GET['act'];
    $db = DB::getInstance();
    
    switch ($act) {
        // 找回密码
        case 'reset':
            if (empty($_POST['user']) || empty($_POST['mobi']) || empty($_POST['newpass'])) {
                json_msg(0, '请填写完整');
            }
            
            $user = $db->escape($_POST['user']);
            $mobi = $db->escape($_POST['mobi']);
            $newpass = md5($_POST['newpass']);
            
            // 验证用户信息
            $sql = "SELECT * FROM user WHERE user = '$user' AND mobi = '$mobi'";
            $userInfo = $db->getRow($sql);
            
            if (!$userInfo) {
                json_msg(0, '账号或手机号错误');
            }
            
            // 更新密码
            $result = $db->update('user', ['pass' => $newpass], "id = {$userInfo['id']}");
            
            if ($result) {
                json_msg(1, '密码重置成功，请返回登录');
            } else {
                json_msg(0, '密码重置失败，请稍后重试');
            }
            break;
            
        default:
            json_msg(0, '未知操作');
    }
    
    exit;
}
include './inc/head.php';
?>

<div class="mima-container" style="max-width:500px;margin:20px auto;background:#fff;padding:20px;border-radius:5px;box-shadow:0 1px 3px rgba(0,0,0,0.1);">
    <h2 style="text-align:center;margin-bottom:20px;">找回密码</h2>
    
    <form id="mima-form">
        <div class="form-group">
            <label class="form-label">账号（邮箱）</label>
            <input type="email" name="user" class="form-control" placeholder="请输入注册邮箱" required>
        </div>
        
        <div class="form-group">
            <label class="form-label">手机号</label>
            <input type="tel" name="mobi" class="form-control" placeholder="请输入注册手机号" required>
        </div>
        
        <div class="form-group">
            <label class="form-label">新密码</label>
            <input type="password" name="newpass" class="form-control" placeholder="请输入新密码" required>
        </div>
        
        <div class="form-group">
            <label class="form-label">确认新密码</label>
            <input type="password" name="renewpass" class="form-control" placeholder="请再次输入新密码" required>
        </div>
        
        <div class="form-group">
            <button type="button" id="submit-btn" class="btn btn-primary">重置密码</button>
            <a href="usr.php?do=login" class="btn btn-default" style="margin-left:10px;">返回登录</a>
        </div>
    </form>
</div>

<script>
document.addEventListener('DOMContentLoaded', function() {
    const submitBtn = document.getElementById('submit-btn');
    const mimaForm = document.getElementById('mima-form');
    
    // 提交按钮点击事件
    submitBtn.addEventListener('click', function() {
        const user = mimaForm.elements.user.value;
        const mobi = mimaForm.elements.mobi.value;
        const newpass = mimaForm.elements.newpass.value;
        const renewpass = mimaForm.elements.renewpass.value;
        
        if (!user) {
            showToast('请输入注册邮箱');
            return;
        }
        
        if (!mobi) {
            showToast('请输入注册手机号');
            return;
        }
        
        if (!newpass) {
            showToast('请输入新密码');
            return;
        }
        
        if (newpass.length < 6) {
            showToast('新密码不能少于6位');
            return;
        }
        
        if (!renewpass) {
            showToast('请再次输入新密码');
            return;
        }
        
        if (newpass !== renewpass) {
            showToast('两次密码不一致');
            return;
        }
        
        // 提交重置
        ajax({
            url: 'usr.php?do=mima&act=reset',
            method: 'POST',
            data: {
                user: user,
                mobi: mobi,
                newpass: newpass
            },
            success: function(res) {
                showToast(res.msg);
                if (res.code === 1) {
                    // 重置成功，2秒后跳转到登录页
                    setTimeout(function() {
                        window.location.href = 'usr.php?do=login';
                    }, 2000);
                }
            },
            error: function() {
                showToast('网络错误，请稍后重试');
            }
        });
    });
});
</script>
